package com.unicom.projt;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.*;
import com.unicom.projt.account.ChangePasswordPage;
import com.unicom.projt.account.RetrievePasswordStepOnePage;
import com.unicom.projt.account.RetrievePasswordStepThreePage;
import com.unicom.projt.db.DB;
import com.unicom.projt.db.DBConfig;
import com.unicom.projt.fee.PaymentInputPhoneNumberPage;
import com.unicom.projt.fee.PaymentPage;
import com.unicom.projt.ivr.BindCardPage;
import com.unicom.projt.ivr.UnBindCardPage;
import com.unicom.projt.query.*;
import com.unicom.projt.reg.RegisterStepTwoPage;
import com.unicom.projt.util.AlertDialogUtils;
import com.unicom.projt.util.PageJumpUtils;
import com.unicom.projt.util.SystemUtils;
import com.unicom.projt.util.UserHolder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: Lee
 * Date: 11-12-5
 * Time: 下午5:02
 */
public class Home extends Activity implements View.OnClickListener {
    private static final String TAG = "PROJ-T";
    private ListView mGridView;
    private List<Map<String, Object>> gridItems = new ArrayList<Map<String, Object>>();
    private Handler mHandler;
    private SimpleAdapter adapter;

    private Context content;


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);
        content = this;
        ((TextView)this.findViewById(R.id.home_number_txt)).setText(UserHolder.phoneNumber);
        initGridViewData();
        initGridViewGUI();
        this.findViewById(R.id.home_logout_btn).setOnClickListener(this);
    }

    private void initGridViewData() {


        Map<String, Object> item = new HashMap<String, Object>();
        item.put("ItemImage", R.drawable.ico_payment);
        item.put("ItemText", "充值缴费");
        item.put("ItemText2", "给自己充值，也可为他人缴费");
        item.put("itemId", 0);
        gridItems.add(item);

        item = new HashMap<String, Object>();
        item.put("ItemImage", R.drawable.ico_current_fee);
        item.put("ItemText", "当月话费");
        item.put("ItemText2", "当月话费明细查询");
        item.put("itemId", 1);
        gridItems.add(item);

        item = new HashMap<String, Object>();
        item.put("ItemImage", R.drawable.ico_query_ar);
        item.put("ItemText", "套餐余量");
        item.put("ItemText2", "当前剩余流量/购买流量包");
        item.put("itemId", 2);
        gridItems.add(item);

        item = new HashMap<String, Object>();
        item.put("ItemImage", R.drawable.ico_history);
        item.put("ItemText", "历史账单");
        item.put("ItemText2", "历史账单明细查询");
        item.put("itemId", 3);
        gridItems.add(item);

        item = new HashMap<String, Object>();
        item.put("ItemImage", R.drawable.ico_query_ab);
        item.put("ItemText", "可用余额");
        item.put("ItemText2", "预付款余额查询");
        item.put("itemId", 4);
        gridItems.add(item);

        item = new HashMap<String, Object>();
        item.put("ItemImage", R.drawable.ico_un_bind_card);
        item.put("ItemText", "缴费记录查询");
        item.put("ItemText2", "可查询过去6个月内的充值记录");
        item.put("itemId", 5);
        gridItems.add(item);

        item = new HashMap<String, Object>();
        item.put("ItemImage", R.drawable.ico_change_pwd);
        item.put("ItemText", "修改密码");
        item.put("ItemText2", "可修改登录密码");
        item.put("itemId", 6);
        gridItems.add(item);
//
//        item = new HashMap<String, Object>();
//        item.put("ItemImage", R.drawable.ico_integral);
//        item.put("ItemText", "积分查询");
//        item.put("itemId", 7);
//        gridItems.add(item);

        item = new HashMap<String, Object>();
        item.put("ItemImage", R.drawable.ico_bind_card);
        item.put("ItemText", "绑定银行卡");
        item.put("ItemText2", "绑定银行卡说明");
        item.put("itemId", 8);
        gridItems.add(item);

        item = new HashMap<String, Object>();
        item.put("ItemImage", R.drawable.projt_unbind_ico);
        item.put("ItemText", "解除绑定");
        item.put("ItemText2", "解除银行卡说明");
        item.put("itemId", 9);
        gridItems.add(item);


    }

    /**
     * 初始化 grid View
     */
    private void initGridViewGUI() {
        adapter = new SimpleAdapter(this, gridItems, R.layout.grid_view_item, new String[]{"ItemImage", "ItemText", "ItemText2"}, new int[]{R.id.ItemImage, R.id.ItemText, R.id.ItemText2});
        mGridView = (ListView) findViewById(R.id.gridView);
        mGridView.setAdapter(adapter);
        mGridView.setOnItemClickListener(new GridListViewClickedListener());
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.home_logout_btn:
                this.onBackPressed();
                break;
        }
    }
    @Override
    public void onBackPressed() {
        AlertDialogUtils.showYesNoDiaLog(this, "是否注销？", new AlertDialogUtils.Executor() {
            @Override
            public void execute() {
                UserHolder.clean();
                cleanAccountTable();
                clickBackBtn();
            }
        });
    }

    private void cleanAccountTable(){
        DB db=DB.getInstance(this);
        db.open();
        db.clearDBTableByName(DBConfig.Account.TABLE_NAME);
        db.close();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_SEARCH) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void clickBackBtn(){
        Intent intent = new Intent(this, InputPhoneNumberPage.class);
        this.startActivity(intent);
        this.finish();
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (UserHolder.phoneNumber.equals("")) {
            this.finish();
        }
    }

    private void finishedThisPage(){
        this.finish();
    }

    class GridListViewClickedListener implements AdapterView.OnItemClickListener {
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            String itemId = gridItems.get(i).get("itemId").toString();
            Log.d(TAG, itemId);
            if (itemId.equals("0")) {
                PageJumpUtils.jump(content, PaymentInputPhoneNumberPage.class);
            } else if (itemId.equals("1")) {
                PageJumpUtils.jump(content, QueryCurrentMonthPage.class);
            } else if (itemId.equals("2")) {
                PageJumpUtils.jump(content, QueryPackageRemainPage.class);
            } else if (itemId.equals("3")) {
                PageJumpUtils.jump(content, QueryHistoryPage.class);
            } else if (itemId.equals("4")) {
                PageJumpUtils.jump(content, QueryAvailableBalancePage.class);
            } else if (itemId.equals("5")) {
               PageJumpUtils.jump(content, QueryPaymentPage.class);
            }else if (itemId.equals("6")) {
                PageJumpUtils.jump(content, ChangePasswordPage.class);
            }else if (itemId.equals("7")) {
//                PageJumpUtils.jump(content, RetrievePasswordStepOnePage.class);
            }else if (itemId.equals("8")) {
                PageJumpUtils.jump(content, BindCardPage.class);
            }else if (itemId.equals("9")) {
                PageJumpUtils.jump(content, UnBindCardPage.class);
            }
            finishedThisPage();
        }
    }
}
